package com.dcits.electronic_account.service.impl;

import com.dcits.electronic_account.bean.VirtualAccount;
import com.dcits.electronic_account.mapper.LoginMapper;
import com.dcits.electronic_account.mapper.UserMapper;
import com.dcits.electronic_account.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

import static com.dcits.electronic_account.utils.DateStampUtil.dateToString;
import static com.dcits.electronic_account.utils.LegalStringCheckUtil.legalStringCheck;
import static com.dcits.electronic_account.utils.MD5Util.getMd5;
import static com.dcits.electronic_account.utils.VerificationCodeUtil.verificationCodeValidate;

/**
 * @Author: CZJ
 * @Date： 2022/6/28
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    LoginMapper loginMapper;

    @Override
    public boolean cancelAccount(VirtualAccount virtualAccount) {
        if (!StringUtils.isEmpty(virtualAccount.getUserId())){
            try{
                userMapper.deleteVirtualAccountByUserId(virtualAccount);
                userMapper.deleteLoginRecordByUserId(virtualAccount);
                userMapper.deleteyVirtualBankByUserId(virtualAccount);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public VirtualAccount getUserInfo(VirtualAccount virtualAccount) {
        if (!StringUtils.isEmpty(virtualAccount.getUserId())){
            return userMapper.getUserInfoById(virtualAccount);
        }
        return null;
    }

    @Override
    public HashMap<Boolean, String> updateUserInfo(VirtualAccount virtualAccount) {
        HashMap<Boolean, String> res = new HashMap<>();

        // 对传入的参数进行判空
        if (!StringUtils.isEmpty(virtualAccount.getUserId()) && !StringUtils.isEmpty(virtualAccount.getUserName())
                && !StringUtils.isEmpty(virtualAccount.getLoginPwd()) && !StringUtils.isEmpty(virtualAccount.getPayPwd())
                && !StringUtils.isEmpty(virtualAccount.getPhone())){

            // 非法字符检测
            boolean b1 = legalStringCheck(virtualAccount.getUserName());
            boolean b2 = legalStringCheck(virtualAccount.getLoginPwd());
            boolean b3 = legalStringCheck(virtualAccount.getPayPwd());
            boolean b4 = legalStringCheck(virtualAccount.getMail());
            boolean b5 = legalStringCheck(virtualAccount.getAddress());
            boolean b6 = legalStringCheck(virtualAccount.getPhone());
            if (!b1 || !b2 || !b3 || !b4 || !b5 || !b6){
                res.put(false, "所填信息中存在非法字符，请检查！");
                return res;
            }

            // 验证数据库中该用户名是否已注册
            String userName = userMapper.userNameExist(virtualAccount);
            if (!StringUtils.isEmpty(userName)){
                res.put(false, "该用户已存在！");
                return res;
            }

            // 验证数据库中该手机号是否已注册
            String phone = userMapper.phoneExist(virtualAccount);
            if (!StringUtils.isEmpty(phone)){
                res.put(false, "该手机号已注册！");
                return res;
            }

            // 对登录密码和支付密码进行MD5加密
            String loginPwd = getMd5(virtualAccount.getLoginPwd());
            String payPwd = getMd5(virtualAccount.getPayPwd());
            virtualAccount.setLoginPwd(loginPwd);
            virtualAccount.setPayPwd(payPwd);

            // 尝试新增记录
            try {
                boolean updateRes = userMapper.updateUserInfo(virtualAccount);
                if (updateRes){
                    res.put(updateRes, "更新成功!");
                    return res;
                }
                res.put(false, "更新失败!");
                return res;
            }catch (Exception e){
                e.printStackTrace();
                res.put(false, "未知错误！");
                return res;
            }
        }
        res.put(false, "信息未填充完整！");
        return res;
    }
}
